package com.niuxiaofa.newlaw.Network;

import android.widget.Toast;

import com.google.gson.Gson;
import com.niuxiaofa.newlaw.Bean.InviteBean;
import com.niuxiaofa.newlaw.Bean.QueryVideoListByLableBean;
import com.niuxiaofa.newlaw.Bean.RankingBean;
import com.niuxiaofa.newlaw.Tools.UIUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LawUniversalPresenter {


    public static void getIncomeList(String todayType, interface_getIncomeList interface_getIncomeList) {

        Call<RankingBean> call = ServiceManager.getInstance().creat(ApiService.class).getIncomeList(todayType);

        call.enqueue(new Callback<RankingBean>() {
            @Override
            public void onResponse(Call<RankingBean> call, Response<RankingBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    RankingBean baseBean = (RankingBean) response.body();
                    interface_getIncomeList.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<RankingBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_getIncomeList {
        void getResponse(RankingBean u);
    }


    public static void getAwardsRanking(String todayType, interface_getAwardsRanking interface_getAwardsRanking) {

        Call<RankingBean> call = ServiceManager.getInstance().creat(ApiService.class).getAwardsRanking(todayType);

        call.enqueue(new Callback<RankingBean>() {
            @Override
            public void onResponse(Call<RankingBean> call, Response<RankingBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    RankingBean baseBean = (RankingBean) response.body();
                    interface_getAwardsRanking.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<RankingBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_getAwardsRanking {
        void getResponse(RankingBean u);
    }


    public static void getPopularityRankList(String todayType, interface_getPopularityRankList interface_getPopularityRankList) {

        Call<RankingBean> call = ServiceManager.getInstance().creat(ApiService.class).getPopularityRankList(todayType);

        call.enqueue(new Callback<RankingBean>() {
            @Override
            public void onResponse(Call<RankingBean> call, Response<RankingBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    RankingBean baseBean = (RankingBean) response.body();
                    interface_getPopularityRankList.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<RankingBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_getPopularityRankList {
        void getResponse(RankingBean u);
    }


    public static void queryVideoListByLable(String lableId, int currentIndex, interface_queryVideoListByLable interface_getPopularityRankList) {

        Call<QueryVideoListByLableBean> call = ServiceManager.getInstance().creat(ApiService.class).queryVideoListByLable(lableId, currentIndex, 5);

        call.enqueue(new Callback<QueryVideoListByLableBean>() {
            @Override
            public void onResponse(Call<QueryVideoListByLableBean> call, Response<QueryVideoListByLableBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    QueryVideoListByLableBean baseBean = (QueryVideoListByLableBean) response.body();
                    interface_getPopularityRankList.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<QueryVideoListByLableBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_queryVideoListByLable {
        void getResponse(QueryVideoListByLableBean u);
    }


    public static void getInvitePagelist(int currentIndex, interface_getInvitePagelist interface_getInvitePagelist) {
        Gson gson = new Gson();
        Map map = new HashMap();
        map.put("current",currentIndex);
        map.put("size",20);
        String json = gson.toJson(map);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Call<InviteBean> call = ServiceManager.getInstance().creat(ApiService.class).getInvitePagelist(body);

        call.enqueue(new Callback<InviteBean>() {
            @Override
            public void onResponse(Call<InviteBean> call, Response<InviteBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    InviteBean baseBean = (InviteBean) response.body();
                    interface_getInvitePagelist.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<InviteBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });

    }

    public interface interface_getInvitePagelist {
        void getResponse(InviteBean u);
    }

}
